package org.jsola.hr.web;

import io.jsonwebtoken.lang.Collections;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.UserRegisterCensorAddDTO;
import org.jsola.hr.dto.UserRegisterCensorAdminUpdateDTO;
import org.jsola.hr.query.UserRegisterCensorQuery;
import org.jsola.hr.service.IUserRegisterCensorService;
import org.jsola.hr.vo.UserRegisterCensorListVO;
import org.jsola.hr.vo.UserRegisterCensorVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 注册审核表
 *
 * @author zhr
 */
@RestController("hrUserRegisterCensorController")
@RequestMapping(HrConstants.API_PATH + "/v1/userRegisterCensors")
public class UserRegisterCensorController implements IUserRegisterCensorControllerAPI {

    private static final Logger log = LoggerFactory.getLogger(UserRegisterCensorController.class);

    @Autowired
    private IUserRegisterCensorService userRegisterCensorService;

    @Override
    @PostMapping
    public Result<UserRegisterCensorVO> save(@Valid @RequestBody UserRegisterCensorAddDTO userRegisterCensorAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增注册审核表权限校验

        return Result.success(
                userRegisterCensorService.save(userRegisterCensorAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody UserRegisterCensorAdminUpdateDTO userRegisterCensorAdminUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        tokenUser.setSiteId("default");
        // todo 彻底删除注册审核表权限校验

        // 更新
        int count = userRegisterCensorService.updateRegisterStatus(userRegisterCensorAdminUpdateDTO,tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{userRegisterCensorId}")
    public Result<Integer> delete(@PathVariable String userRegisterCensorId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除注册审核表权限校验

        int count = userRegisterCensorService.deleteByIds(tokenUser, userRegisterCensorId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] userRegisterCensorIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除注册审核表权限校验

        int count = userRegisterCensorService.deleteByIds(tokenUser, userRegisterCensorIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{userRegisterCensorId}")
    public Result<UserRegisterCensorVO> findById(@PathVariable String userRegisterCensorId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看注册审核表权限校验

        return Result.success(
            userRegisterCensorService.selectById(userRegisterCensorId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<UserRegisterCensorListVO>> listAll(@Valid UserRegisterCensorQuery userRegisterCensorQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看注册审核表权限校验

        return Result.success(
            userRegisterCensorService.select(userRegisterCensorQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<UserRegisterCensorListVO>> page(@Valid UserRegisterCensorQuery userRegisterCensorQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            userRegisterCensorService.selectPage(userRegisterCensorQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping("/submitRegisterReview")
    public Result<Integer> submitRegistrationReview(@Valid @RequestBody UserRegisterCensorAddDTO userRegisterCensorAddDTO) {

        // 先查询是否已注册 再保存数据
        return Result.success(
                userRegisterCensorService.insertRegisterInfo(userRegisterCensorAddDTO)
        );
    }

    @Override
    @GetMapping("/unauditedCount")
    public Result<Integer> unauditedCount(@Valid UserRegisterCensorQuery userRegisterCensorQuery) {
        List<UserRegisterCensorListVO> list = userRegisterCensorService.select(userRegisterCensorQuery, null);
        Integer num = 0;
        if (!Collections.isEmpty(list)) {
            num = list.size();
        }
        return Result.success(num);
    }

}

